import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '@/views/home'

import management from '@/router/modules/management'
import order from './modules/order'
Vue.use(VueRouter)
const VueRouterPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (to) {
  return VueRouterPush.call(this, to).catch(err => err)
}
const constantRoutes = [
  // 登陆
  { path: '/login', component: () => import('@/views/login') },
  // 首页
  {
    path: '/',
    component: Home,
    redirect: '/home',
    children: [
      { path: '/home', component: () => import('@/components/subject/home') }
    ]
  },
  // 广告
  { path: '/advertisement', component: () => import('@/views/advertisement') },
  // 404页面
  { path: '*', component: () => import('@/views/erroe') }
]
// 动态路由
const asyncRoutes = [management, order]
// 静态路由
const createRouter = () => new VueRouter({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: [...constantRoutes, ...asyncRoutes]
})
const router = createRouter()
export function resetRouter () {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
